package com.example.ai.inventory.controller;

import com.example.ai.inventory.entity.PurchaseOrder;
import com.example.ai.inventory.entity.PurchaseItem;
import com.example.ai.inventory.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 采购控制器
 */
@RestController
@RequestMapping("/api/purchases")
public class PurchaseController {
    
    @Autowired
    private PurchaseService purchaseService;
    
    /**
     * 创建采购单
     */
    @PostMapping
    public PurchaseOrder createPurchaseOrder(@RequestBody PurchaseOrder order) {
        return purchaseService.createPurchaseOrder(order);
    }
    
    /**
     * 根据ID获取采购单
     */
    @GetMapping("/{id}")
    public PurchaseOrder getPurchaseOrderById(@PathVariable Long id) {
        return purchaseService.getPurchaseOrderById(id);
    }
    
    /**
     * 根据采购单号获取采购单
     */
    @GetMapping("/orderNo/{orderNo}")
    public PurchaseOrder getPurchaseOrderByOrderNo(@PathVariable String orderNo) {
        return purchaseService.getPurchaseOrderByOrderNo(orderNo);
    }
    
    /**
     * 获取采购单列表
     */
    @GetMapping
    public List<PurchaseOrder> getPurchaseOrders(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        return purchaseService.getPurchaseOrders(startTime, endTime);
    }
    
    /**
     * 获取采购明细
     */
    @GetMapping("/{id}/items")
    public List<PurchaseItem> getPurchaseItemsByOrderId(@PathVariable Long id) {
        return purchaseService.getPurchaseItemsByOrderId(id);
    }
    
    /**
     * 获取商品的采购历史
     */
    @GetMapping("/product/{productId}")
    public List<PurchaseItem> getPurchaseHistoryByProductId(@PathVariable Long productId) {
        return purchaseService.getPurchaseHistoryByProductId(productId);
    }
}